<?php

//apd_set_pprof_trace();
require_once('../includes/DB.class.php');
require_once('../includes/SmartyManager.class.php');
require_once('../includes/exceptions/AppExceptions.class.php');
require_once('../includes/SimpleTableExporter.class.php');

$filename = '总表未完成_'.date('YmdHis').'.xls';

ini_set("max_execution_time",0);
ini_set( "memory_limit" , "128M");

try{
	$exp = new SimpleTableExporter($filename);
	$query = "select * from
	(
		(
			select materialCode,orderNumber,orderSubitemNumber,sum(`count`) as sumCount3 from little_main3 group by orderNumber,orderSubitemNumber
		) as main3Table
		left join 
		(
			select *,sum(`count`) as sumCount from little_main group by orderNumber,orderSubitemNumber
		)as mainTable
		using (orderNumber,orderSubitemNumber)
	)
	where main3Table.sumCount3 > mainTable.sumCount;";


$_materialCode = array();
$_main3Count = array();
$_main3SumCount = array();
$_main3AllocatedCount = array();
$_mainAllocatedCount = array();
$_mainCount = array();
$_mainSumCount = array();
$_mainCertificationCount = array();
$_sequenceNumber = array();
$_manufactory = array();
$_authenticationMark = array();
$_steelNumber =  array();
$_size1 = array();
$_size2 = array();
$_size3 = array();
$_unitWeight = array();
$_orderNumber = array();
$_orderSubitemNumber = array();
$_unitPrice = array();
$_purchaseNumber = array();
$_remark = array();
$_certificationNumber = array();
$_sampleBatch = array();
$_certificationCodification = array();
$_facheNumber = array();
$_materialNumber = array();
$_consignmentBatch = array();
$_shipmentDate = array();
$_month = array();
$_vehicleNumber = array();
$_consignee = array();
$_filename = array();
$_uploadTime = array();

$result = DB::query($query);
if($result == false){
	return;
}

while(($row = $result->fetch_assoc())!=null){
	$main3Sql = "select * from
			(
			select *,sum(`count`) as main3Count from little_main3
			where orderNumber = '{$row['orderNumber']}' and orderSubitemNumber = '{$row['orderSubitemNumber']}'
			group by materialCode
			order by main3Count
			)as main3Table
			 
			left outer join
			
			(
			select orderNumber, orderSubitemNumber,sum(`count`) as main3SumCount from little_main3
			group by orderNumber, orderSubitemNumber
			)as main3SumTable
			using (orderNumber, orderSubitemNumber)
			";
	$main3Result = DB::query($main3Sql);
	$materialCode = array();
	$main3Count = array();
	$main3SumCount = array();
	$sequenceNumber = array();
 	$manufactory = array();
	$authenticationMark = array();
	$steelNumber =  array();
	$size1 = array();
 	$size2 = array();
	$size3 = array();
	$unitWeight = array();
	$orderNumber = array();
	$orderSubitemNumber = array();
	$unitPrice = array();
	$purchaseNumber = array();
	$remark = array();
	$filename = array();
	$uploadTime = array();
	while(($main3Row = $main3Result->fetch_assoc())!=null){
		array_push($materialCode,$main3Row['materialCode']);
		array_push($main3Count, $main3Row['main3Count']);
		array_push($main3SumCount, $main3Row['main3SumCount']);
		array_push($sequenceNumber, $main3Row['sequenceNumber']);
		array_push($authenticationMark, $main3Row['authenticationMark']);
		array_push($manufactory, $main3Row['manufactory']);
		array_push($steelNumber, $main3Row['steelNumber']);
		array_push($size1, $main3Row['size1']);
		array_push($size2, $main3Row['size2']);
		array_push($size3, $main3Row['size3']);
		array_push($unitWeight, $main3Row['unitWeight']);
		array_push($orderNumber, $main3Row['orderNumber']);
		array_push($orderSubitemNumber, $main3Row['orderSubitemNumber']);
		array_push($unitPrice, $main3Row['unitPrice']);
		array_push($purchaseNumber, $main3Row['purchaseNumber']);
		array_push($remark, $main3Row['remark']);
		array_push($filename, $main3Row['filename']);
		array_push($uploadTime, $main3Row['uploadTime']);
	}
	
	$mainSql = "select * from
			(
			select *,sum(`count`) as mainCount from little_main
			where orderNumber = '{$row['orderNumber']}' and orderSubitemNumber = '{$row['orderSubitemNumber']}'
			group by certificationNumber, sampleBatch
			order by mainCount
			)as mainTable
			
			left outer join
			
			(
			select orderNumber, orderSubitemNumber,sum(`count`) as mainSumCount from little_main
			group by orderNumber, orderSubitemNumber
			)as mainSumTable
			using (orderNumber, orderSubitemNumber)
			
			left outer join
			
			(
			select certificationNumber,orderNumber, orderSubitemNumber, sum(`count`) as mainCertificationCount
			from little_main
			group by certificationNumber
			)as mainCertificationTable
			using (certificationNumber, orderNumber, orderSubitemNumber)
			";
	$mainResult = DB::query($mainSql);
	$certificationNumber = array();
	$sampleBatch = array();
	$certificationCodification = array();
	$facheNumber = array();
	$materialNumber = array();
	$consignmentBatch = array();
	$shipmentDate = array();
	$month = array();
	$vehicleNumber = array();
	$consignee = array();
	$mainCount = array();
	$mainSumCount = array();
	$mainCertificationCount = array();
	while(($mainRow = $mainResult->fetch_assoc())!=null){
		array_push($certificationNumber, $mainRow['certificationNumber']);
		array_push($sampleBatch, $mainRow['sampleBatch']);
		array_push($certificationCodification, $mainRow['certificationCodification']);
		array_push($facheNumber, $mainRow['facheNumber']);
		array_push($materialNumber, $mainRow['materialNumber']);
		array_push($consignmentBatch, $mainRow['consignmentBatch']);
		array_push($shipmentDate, $mainRow['shipmentDate']);
		array_push($month, $mainRow['month']);
		array_push($vehicleNumber, $mainRow['vehicleNumber']);
		array_push($consignee, $mainRow['consignee']);
		array_push($mainCount, $mainRow['mainCount']);
		array_push($mainSumCount,$mainRow['mainSumCount']);
		array_push($mainCertificationCount,$mainRow['mainCertificationCount']);
	}

	//是否需要对mainCountTemp重新赋值
	$resetMainCountTemp = true;
	
	//分配原则算法
	for ($main3Key = count($main3Count)-1;$main3Key >=0 ;$main3Key--){
		$main3CountTemp = $main3Count[$main3Key];
		$main3AllocatedCountTemp = 0;
		$loopCount = 0;
		for($mainKey = count($mainCount)-1;$mainKey >=0 ;$mainKey--,$loopCount++) {
			if($resetMainCountTemp)
				$mainCountTemp = $mainCount[$mainKey];
			array_push($_mainSumCount, $mainSumCount[$mainKey]);
			array_push($_mainCount,$mainCount[$mainKey]);
			array_push($_mainCertificationCount, $mainCertificationCount[$mainKey]);
			array_push($_certificationNumber,$certificationNumber[$mainKey]);
			array_push($_sampleBatch,$sampleBatch[$mainKey]);
			array_push($_certificationCodification,$certificationCodification[$mainKey]);
			array_push($_facheNumber,$facheNumber[$mainKey]);
			array_push($_materialNumber,$materialNumber[$mainKey]);
			array_push($_consignmentBatch,$consignmentBatch[$mainKey]);
			array_push($_shipmentDate,$shipmentDate[$mainKey]);
			array_push($_month,$month[$mainKey]);
			array_push($_vehicleNumber,$vehicleNumber[$mainKey]);
			array_push($_consignee,$consignee[$mainKey]);
			array_push($_materialCode,$materialCode[$main3Key]);
			array_push($_main3SumCount,$main3SumCount[$main3Key]);
			array_push($_main3Count,$main3Count[$main3Key]);
			array_push($_sequenceNumber,$sequenceNumber[$main3Key]);
			array_push($_manufactory,$manufactory[$main3Key]);
			array_push($_authenticationMark,$authenticationMark[$main3Key]);
			array_push($_steelNumber,$steelNumber[$main3Key]);
			array_push($_size1,$size1[$main3Key]);
			array_push($_size2,$size2[$main3Key]);
			array_push($_size3,$size3[$main3Key]);
			array_push($_unitWeight,$unitWeight[$main3Key]);
			array_push($_orderNumber,$orderNumber[$main3Key]);
			array_push($_orderSubitemNumber,$orderSubitemNumber[$main3Key]);
			array_push($_unitPrice,$unitPrice[$main3Key]);
			array_push($_purchaseNumber,$purchaseNumber[$main3Key]);
			array_push($_remark,$remark[$main3Key]);
			array_push($_filename,$filename[$main3Key]);
			array_push($_uploadTime,$uploadTime[$main3Key]);
			if($main3CountTemp<$mainCountTemp){
				array_push($_mainAllocatedCount,$main3CountTemp);
				array_push($_main3AllocatedCount,$main3CountTemp);
				$mainCountTemp = $mainCountTemp - $main3CountTemp;
				array_pop($main3Count);
				$resetMainCountTemp = false;
				continue 2;
			}else if($main3CountTemp>$mainCountTemp){
				array_push($_mainAllocatedCount,$mainCountTemp);
				array_push($_main3AllocatedCount,$mainCountTemp);
				$main3CountTemp = $main3CountTemp - $mainCountTemp;
				if($loopCount>0)
					$main3AllocatedCountTemp = $main3AllocatedCountTemp + $mainCountTemp;
				else 
					$main3AllocatedCountTemp = $mainCountTemp;
				array_pop($mainCount);
				$resetMainCountTemp = true;
				continue;
			}else if($main3CountTemp==$mainCountTemp){
				array_push($_mainAllocatedCount,$mainCountTemp);
				array_push($_main3AllocatedCount,$mainCountTemp);
				array_pop($main3Count);
				array_pop($mainCount);
				$resetMainCountTemp = true;
				continue 2;
			}
		}
		if(empty($mainCount)){
			array_push($_mainAllocatedCount, 0);
			array_push($_mainSumCount, 0);
			array_push($_mainCount,0);
			array_push($_mainCertificationCount,0);
			array_push($_certificationNumber,'');
			array_push($_sampleBatch,'');
			array_push($_certificationCodification,'');
			array_push($_facheNumber,'');
			array_push($_materialNumber,'');
			array_push($_consignmentBatch,'');
			array_push($_shipmentDate,'');
			array_push($_month,'');
			array_push($_vehicleNumber,'');
			array_push($_consignee,'');
			array_push($_materialCode,$materialCode[$main3Key]);
			array_push($_main3SumCount,$main3SumCount[$main3Key]);
			array_push($_main3Count,$main3Count[$main3Key]);
			array_push($_main3AllocatedCount,$main3Count[$main3Key] - $main3AllocatedCountTemp);
			array_push($_sequenceNumber,$sequenceNumber[$main3Key]);
			array_push($_manufactory,$manufactory[$main3Key]);
			array_push($_authenticationMark,$authenticationMark[$main3Key]);
			array_push($_steelNumber,$steelNumber[$main3Key]);
			array_push($_size1,$size1[$main3Key]);
			array_push($_size2,$size2[$main3Key]);
			array_push($_size3,$size3[$main3Key]);
			array_push($_unitWeight,$unitWeight[$main3Key]);
			array_push($_orderNumber,$orderNumber[$main3Key]);
			array_push($_orderSubitemNumber,$orderSubitemNumber[$main3Key]);
			array_push($_unitPrice,$unitPrice[$main3Key]);
			array_push($_purchaseNumber,$purchaseNumber[$main3Key]);
			array_push($_remark,$remark[$main3Key]);
			array_push($_filename,$filename[$main3Key]);
			array_push($_uploadTime,$uploadTime[$main3Key]);
		}
	}
}

$exp->insertCustomData(array($_materialCode,$_certificationNumber,$_certificationCodification, $_facheNumber, $_sequenceNumber, 
							  $_manufactory, $_authenticationMark, $_steelNumber, $_size1, $_size2, $_size3, $_unitPrice,
							  $_orderNumber, $_orderSubitemNumber, $_unitPrice,$_materialNumber, 
							  $_consignmentBatch, $_sampleBatch,$_purchaseNumber,$_shipmentDate,
							  $_month,$_vehicleNumber, $_consignee, $_remark, $_filename, $_uploadTime,
							  $_main3AllocatedCount, $_main3Count, $_main3SumCount, $_mainAllocatedCount,
							  $_mainCount, $_mainCertificationCount, $_mainSumCount), 
						array('材料代码','产品质量证明书号','产品质量证明书编号','发车证书号', '批次',
							  '生产厂家','船级','材质','厚','宽','长','单重',
							  '订单号','订单子项号','受订单价','物料号',
							  '发货批次','批号','购单号','发货时间',
							  '合同月份','车号','销售产家','备注','文件名','上传时间',
							  '订货清单数量','材料代码数量','订货清单总数量','质量证书分配数量',
					 		  '批号数量','质量证书号数量','质量证书号总数量'), 'sheet1');
$exp->export();

}catch(Exception $e){
	$smarty = SmartyManager::getSmarty();
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}
?>

